﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using MobiSms.Infrastructure;
using MobiSms.WebUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using MobiSms.Library.Extensions;
using MobiSms.DataModel;
using System.Reflection;
using MobiSms.Library.Helpers;
using MobiSms.Enums;

namespace MobiSms.ViewModel
{
    public class ViewModelBase : NotificationObject
    {
        public WebServiceSoapClient SoapClient;
        public IDataContext Database;

        public ViewModelBase()
        {
            Database = new IDataContext(ConstKeys.ConnectionString);
            SoapClient = new WebServiceSoapClient(WebBindingConfig.HttpBinding, new EndpointAddress(WebBindingConfig.Url));
            if (!IsDesignMode)
            {
                GlobalSettings.SettingsChanged -= GlobalSettingsChanged;
                GlobalSettings.SettingsChanged += GlobalSettingsChanged;
            }
            //Invoke(typeof(SampleData), "Insert", null);
        }

        #region Methods
        private void GlobalSettingsChanged(object sender, PropertyArgs e)
        {
            GlobalSettings.Save();
            if (e.Property.Name == "Language")
            {
                Assembly ass = Assembly.Load("MobiSms.Wasun");
                Type appType = ass.GetType("MobiSms.Wasun.App");

                ConstructorInfo constructor = appType.GetConstructor(Type.EmptyTypes);

                MethodInfo method = appType.GetMethod("LoadLanguage");
                if (method != null)
                {
                    method.Invoke(null, null);
                    //MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_LANGUAGE_APPLIED));
                }
            }
        }

        public virtual bool Submit()
        {
            try
            {
                Database.SubmitChanges(ConflictMode.FailOnFirstConflict);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogInfo("SQL", ex));
                return false;
            }
        }

        public virtual void NavigateBack()
        {
            PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual;
            if (frame.CanGoBack)
            {
                frame.GoBack();
            }
        }

        public virtual void Navigate(Uri uri)
        {
            PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual;
            frame.Navigate(uri);
        }

        public virtual object Invoke(Type type, string methodName, object[] parameters)
        {
            ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
            object classObject = constructor.Invoke(new object[] { });
            MethodInfo method = type.GetMethod(methodName);
            if (method != null)
            {
                if (method.IsStatic)
                {
                    return method.Invoke(null, null);
                }
                else
                {
                    return method.Invoke(classObject, null);
                }
            }
            return null;
        }

        public virtual void ShowResult<T>(object code)
        {
            var result = (T)code;
            MessageBox.Show(Application.Current.FindResource<string>(string.Format("IDS_ENUM_{0}", result.ToString().ToUpper())));
        }
        #endregion

        #region Common Properties
        public bool IsDesignMode
        {
            get
            {
                return DesignerProperties.IsInDesignTool;//(bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
            }
        }

        public string LogoUri
        {
            get
            {
                Visibility visibility = Application.Current.FindResource<Visibility>("PhoneDarkThemeVisibility");
                if (visibility == Visibility.Visible)
                {
                    return "/MobiSms.Wasun;component/Assets/Logo_Light.png";
                }
                return "/MobiSms.Wasun;component/Assets/Logo_Dark.png";
            }
        }

        private OptionInfo globalSettings = new OptionInfo();
        public OptionInfo GlobalSettings
        {
            get { return globalSettings; }
            set
            {
                globalSettings = value;
                RaisePropertyChanged(() => GlobalSettings);
            }
        } 
        #endregion
    }
}
